package zzps.exeed.web.transformer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import zzps.exeed.web.Context;

import java.io.IOException;
import java.io.OutputStream;

public class JacksonInputOutputTransformer implements InputOutputTransformer{
    ObjectMapper objectMapper = new ObjectMapper();

    public JacksonInputOutputTransformer() {
        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.registerModule(new Jdk8Module());
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
    }

    @Override
    public void input(Class<?> clazz,Context context) {

    }

    @Override
    public void output(Object object,Context context){
        OutputStream outputStream = context.getOutputStream();
        try {
            objectMapper.writeValue(outputStream,object);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
